﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fluq
{
	sealed class ContextualDependencyResolver : IDependencyResolver
	{
		private object _identifier;
		private IDependencyResolver _resolver;

		public ContextualDependencyResolver(object graphId, IDependencyResolver resolver)
		{
			_identifier = graphId;
			_resolver = resolver;
		}

		public bool TryResolve(object graphId, IDependencyContainer container, IDependencyKey key, out object instance)
		{
			if (_identifier != graphId)
			{
				throw new Exception("Called from root");
			}

			return _resolver.TryResolve(graphId, container, key, out instance);
		}

		public bool TryResolve(IDependencyContainer container, IDependencyKey key, out object instance)
		{
			return _resolver.TryResolve(_identifier, container, key, out instance);
		}

		public bool TryRelease(IDependencyContainer container, object instance)
		{
			return _resolver.TryRelease(container, instance);
		}
	}
}
